# 学生选课系统 - 根目录 Makefile
# 作者: CMO

# 编译器配置
CC = gcc
CFLAGS = -Wall -g -I./Include
LDFLAGS = -L./RELEASE

# 产品形态配置
ifdef FROM_FILE
    CFLAGS += -DFROM_FILE
endif

# 目标文件配置
TARGET = ./RELEASE/student_system
MODULES = ModuleStudent ModuleCourse ModuleFileIO

# 默认目标
all: $(TARGET)

# 链接生成可执行文件
$(TARGET): Main.o $(MODULES)
	@echo "链接生成可执行文件..."
	$(CC) -o $@ ./RELEASE/Main.o $(LDFLAGS) -lstudent -lcourse -lfileio
	@echo "可执行文件生成完成: $(TARGET)"

# 编译主程序
Main.o: Main.c Main.h
	@echo "编译主程序..."
	$(CC) $(CFLAGS) -c Main.c -o ./RELEASE/Main.o

# 编译各模块
$(MODULES):
	@echo "编译模块: $@"
	$(MAKE) -C $@

# 清理编译结果
clean:
	@echo "清理编译结果..."
	rm -rf ./RELEASE/*.o ./RELEASE/$(TARGET) ./RELEASE/*.a ./RELEASE/*.so
	@for dir in $(MODULES); do \
		echo "清理模块: $$dir"; \
		$(MAKE) -C $$dir clean; \
	done
	@echo "清理完成"

# 安装到系统目录 (可选)
install: all
	@echo "安装到系统目录..."
	cp $(TARGET) /usr/local/bin/student_system
	@echo "安装完成，可通过 'student_system' 命令运行"

# 调试版本
debug: CFLAGS += -DDEBUG -O0
debug: all

# 发布版本
release: CFLAGS += -O2
release: all

.PHONY: all clean install debug release $(MODULES)
